package com.daka.service.impl;

import com.daka.dao.OrganizationMapper;
import com.daka.entity.Organization;
import com.daka.enumerate.CommonStatus;
import com.daka.enumerate.StatusCode;
import com.daka.service.OrganizationCountService;
import com.daka.service.OrganizationService;
import com.daka.vo.db.OrgCountVo;
import com.daka.vo.response.DataResponseVo;
import com.daka.vo.response.org.OrgCountResponseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author beliveli on 2016/3/31 0031.
 */
@Service("organizationService")
@SuppressWarnings("unused")
public class OrganizationServiceImpl implements OrganizationService {

    @Resource(name = "organizationMapper")
    private OrganizationMapper organizationMapper;

    @Resource
    private OrganizationCountService organizationCountService;

    public Organization selectById(int orgId) {
        return organizationMapper.selectById(orgId);
    }

    public List<Organization> selectAllChild(int orgId) {
        return organizationMapper.selectAllChild(orgId);
    }

    public List<Organization> selectAllChildNoSelf(int orgId){
        return organizationMapper.selectAllChildNoSelf(orgId);
    }

    public List<Organization> selectAllChild(int orgId, String orgName, int pageIndex) {
        int pageSize = 10;
        return organizationMapper.selectAllChildByName(orgId, orgName, (pageIndex - 1) * pageSize, pageSize);
    }

    public int selectCount(int orgId, String orgName) {
        return organizationMapper.selectCountByName(orgId, orgName);
    }

    public List<Organization> selectChild(int orgId) {
        return organizationMapper.selectChild(orgId);
    }


    public Organization save(Organization organization, int orgParent, int userId) {

        if (organization.getOrgParent() == 0) {
            organization.setOrgParent(orgParent);
        }

        Organization parentOrg = organizationMapper.selectById(organization.getOrgParent());

        int resultCount;
        if (organization.getOrgId() == 0) {

            organization.setOnePass(this.getOrgOnePass());
            organization.setCreateTime(new Timestamp(new Date().getTime()));
            organization.setStatus(CommonStatus.NORMAL.getStatusCode());
            organization.setOrgPath(parentOrg.getOrgPath());
            organization.setPlatformId(parentOrg.getPlatformId());

            organization.setCreateUser(userId);

            resultCount = organizationMapper.save(organization);

            if (resultCount == 0) {
                return null;
            }

//            if (parentOrg.getOrgId() != parentOrg.getOrgParent()) {
                Organization temp = new Organization();
                temp.setOrgId(organization.getOrgId());
                temp.setOrgPath(parentOrg.getOrgPath() + "{" + organization.getOrgId() + "},");
                organizationMapper.updateById(temp);
//            }
        } else {
            resultCount = organizationMapper.updateById(organization);
        }

        if (resultCount == 0) {
            return null;
        }
        return organization;
    }

    public int updateById(Organization organization) {
        return organizationMapper.updateById(organization);
    }

    public Organization updateStatus(Integer orgId) {

        Organization result = organizationMapper.selectById(orgId);

        if (result == null) {
            return null;
        }

        int resultCount;
        if (result.getStatus() == CommonStatus.NORMAL.getStatusCode()) {
            result.setStatus(CommonStatus.DISABLED.getStatusCode());
            resultCount = organizationMapper.updateStatus(CommonStatus.DISABLED.getStatusCode(), orgId);
        } else {
            result.setStatus(CommonStatus.NORMAL.getStatusCode());
            resultCount = organizationMapper.updateStatus(CommonStatus.NORMAL.getStatusCode(), orgId);
        }

        if (resultCount == 0) {
            return null;
        }

        return result;
    }

    public DataResponseVo<OrgCountResponseVo> count(int orgId) {
        Organization organization = organizationMapper.selectById(orgId);
        List<OrgCountVo> orgCountVoList = organizationCountService.selectByMonth(orgId);
        OrgCountVo orgIndex = organizationCountService.selectIndexCount(orgId,organization.getPlatformId(), organization.getJurisdiction());
        String volunteerStr = "";
        String timeStr = "";
        String activityStr = "";
        String groupStr = "";

        for (OrgCountVo vo : orgCountVoList) {
            volunteerStr = volunteerStr + vo.getVolunteerCount() + ",";
            timeStr = timeStr + vo.getTimeCount() + ",";
            activityStr = activityStr + vo.getActivityCount() + ",";
            groupStr = groupStr + vo.getGroupCount()+ ",";
        }
        if (volunteerStr.length() != 0) {
            volunteerStr = volunteerStr.substring(0, volunteerStr.length() - 1);
        }
        if (timeStr.length() != 0) {
            timeStr = timeStr.substring(0, timeStr.length() - 1);
        }
        if (activityStr.length() != 0) {
            activityStr = activityStr.substring(0, activityStr.length() - 1);
        }
        if (groupStr.length() != 0) {
            groupStr = groupStr.substring(0, groupStr.length() - 1);
        }

        OrgCountResponseVo resultVo = OrgCountResponseVo.build(orgIndex,groupStr,activityStr,timeStr,volunteerStr);
        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), resultVo);
    }


    private String getOrgOnePass() {
        return String.valueOf(System.currentTimeMillis()).substring(9) + UUID.randomUUID().toString().substring(9, 11);
    }
}
